package structure;


import java.util.ArrayList;



public class Group {
	private byte color;
	private ArrayList<Coordinate> stones = new ArrayList<Coordinate>();
	private ArrayList<Coordinate> liberties = new ArrayList<Coordinate>();
	
	public void addStone(Coordinate c){
		stones.add(c);
	}
	
	public byte getColor(){
		return color;
	}
	
	public int size(){
		return stones.size();
	}
	
	public boolean isLibertyOf(Coordinate n){
		return liberties.indexOf(n) != -1;
	}
	
	public void addLiberty(Coordinate n){
		if(!isLibertyOf(n))
			liberties.add(n);
	}
	
	public void setLiberties(ArrayList<Coordinate> liberties){
		this.liberties = liberties;
	}
	
	public static Group merge(ArrayList<Group> groups, SympleBoard board){
		Group r = new Group();
		Group a;
		for(int i = 0; i<groups.size(); i++){
			a = groups.get(i);
			for(Coordinate stone : a.stones){
				r.stones.add(stone);
				board.setGroupAt(r, stone);
			}
			
			for(Coordinate c : a.liberties){
				r.addLiberty(c);
			}
			
			board.removeGroup(a);
		}
		
		board.addGroup(r);
		
		return r;
	}
	
	public Group cloneTo(SympleBoard board){
		Group group = new Group();
		group.color = color;
		ArrayList<Coordinate> stones = new ArrayList<Coordinate>();
		
		for(Coordinate c : this.stones){
			stones.add(c);
			board.setGroupAt(group, c);
		}
		
		group.stones = stones;
		
		ArrayList<Coordinate> liberties = new ArrayList<Coordinate>();
		for(Coordinate c : this.liberties)
			liberties.add(c);
		
		group.liberties = liberties;
		
		return group;
	}

	public void removeLiberty(Coordinate c) {
		liberties.remove(c);
	}

	public ArrayList<Coordinate> getLiberties() {
		return liberties;
	}
}
